package com.hadoop.mapreduce.mobilestatistics.map;


import com.hadoop.mapreduce.mobilestatistics.bean.MobileTrafficInformation;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;


/**
 * @author chenxigu
 */
public class MobileMap extends Mapper<LongWritable, Text, Text, MobileTrafficInformation> {

    @Override
    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        List<String> textData = Arrays.asList(line.split("\t"));
        MobileTrafficInformation data = assemblyData(textData);
        context.write(new Text(data.getPhone()), data);
    }


    private MobileTrafficInformation assemblyData(List<String> textData) {
        MobileTrafficInformation data= null;
        try {
             data =
                    MobileTrafficInformation.builder()
                            .time(Long.parseLong(textData.get(0).trim()))
                            .phone(textData.get(1).trim())
                            .physicalAddress(textData.get(2).trim())
                            .ip(textData.get(3).trim())
                            .domainName(textData.get(4).trim())
                            .data(textData.get(5).trim())
                            .receivePackageCount(Long.parseLong(textData.get(6).trim()))
                            .upstreamDownstreamTraffic(Long.parseLong(textData.get(8).trim()))
                            .downLinkAmpacity(Long.parseLong(textData.get(9).trim()))
                            .responseCode(textData.get(10).trim()).build();
        }catch (Exception e){
            e.printStackTrace();
        }
        return data;
    }
}
